﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.Xml;
using System.IO;
using System.Timers;
using DLL.Entities;

namespace MIS
{
    public partial class Form1 : Form
    {
        // Array to set the number of available missiles to 5.
        Missile[] Missiles = new Missile[5];
        WriteXML xWrite = new WriteXML(); 
        ReadXML xRead = new ReadXML(); 
        MIS_Config configM = new MIS_Config();
        int counter;


        private void Form1_Load(object sender, EventArgs e)
        {
            timeWrite.Enabled = true;
        }



        private void tWrite_Tick_1(object sender, EventArgs e)
        {
            xWrite.WriteMISStatus("Online", Missiles[cBoxMissileChoice.SelectedIndex].MissileRemaining, Missiles[cBoxMissileChoice.SelectedIndex].MissileType, "../../../DLL/XML/MIS_Status.xml");
        }           

        

        // Tells COM that MIS is offline
        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            timeWrite.Enabled = false;
            xWrite.WriteMISStatus("Offline", 0, "N/A", "../../../DLL/XML/Mis_Status.xml");
        }

        private void countdown_Tick(object sender, EventArgs e)
        {
            txtMissileImpact.Text = Convert.ToString(--counter);
        }

        public Form1()
        {
            InitializeComponent();

            timeWrite.Enabled = true;
  
            //Deserializes XML file
            configM = xRead.ReadMISConfig();
            txtMissileVersion.Text = configM.Version;
            txtMissileModel.Text = configM.Model;
          
            Missiles = xRead.ReadMissile("../../../DLL/XML/Missile.xml");
            txtMissileSpeed.Text = Convert.ToString(Missiles[0].MissileSpeed);
            txtMissileWeight.Text = Convert.ToString(Missiles[0].MissileWeight);
            txtMissileID.Text = Convert.ToString(Missiles[0].MissileID);
            txtMissileRemaining.Text = Convert.ToString(Missiles[0].MissileRemaining);
            txtMissileName.Text = Missiles[0].MissileName;
            txtMissileRange.Text = Convert.ToString(Missiles[0].MissileRange);


            for (int i = 0; i < 5; i++)
            {
                cBoxMissileChoice.Items.Add(Missiles[i].MissileName);
            }

            cBoxMissileChoice.SelectedIndex = 0;

            txtMissileModel.Text = Convert.ToString(configM.Model);
            txtMissileVersion.Text = Convert.ToString(configM.Version);

            
            xWrite.WriteMISStatus("Online", Missiles[cBoxMissileChoice.SelectedIndex].MissileRemaining, Missiles[cBoxMissileChoice.SelectedIndex].MissileType, "../../../DLL/XML/Mis_Status.xml");

        }
        

        // Drop-down box that displays the missiles available for launch.
        private void cmboxMissiles_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtMissileID.Text = Missiles[cBoxMissileChoice.SelectedIndex].MissileID;
            txtMissileName.Text = Missiles[cBoxMissileChoice.SelectedIndex].MissileName;
            txtMissileRemaining.Text = Convert.ToString(Missiles[cBoxMissileChoice.SelectedIndex].MissileRemaining);
            txtMissileRange.Text = Convert.ToString(Missiles[cBoxMissileChoice.SelectedIndex].MissileRange);
            txtMissileSpeed.Text = Convert.ToString(Missiles[cBoxMissileChoice.SelectedIndex].MissileSpeed);
            txtMissileWeight.Text = Convert.ToString(Missiles[cBoxMissileChoice.SelectedIndex].MissileWeight);
        }

       

        // Gives command to fire
        private void btnFire_Click(object sender, EventArgs e)
        {
            Missiles[cBoxMissileChoice.SelectedIndex].MissileRemaining -= 1;
            txtMissileRemaining.Text = Convert.ToString(Missiles[cBoxMissileChoice.SelectedIndex].MissileRemaining);
            timeImpact.Enabled = true;
            timeCountdown.Enabled = true;
            counter = 10;
            fireMessageBox.Text = "Missile firing now";
            txtMissileImpact.Text = "10";
            
        }  

        // Button to give command to abort missile launch.
        private void btnAbort_Click(object sender, EventArgs e)
        {
            timeImpact.Enabled = false;
            timeCountdown.Enabled = false;
            txtMissileImpact.Text = "";
            fireMessageBox.Text = "You have aborted the Missile!";

        }  

        // Defines the time to Impact
        private void impact_Tick(object sender, EventArgs e)
        {
            timeImpact.Enabled = false;
            timeCountdown.Enabled = false;
            fireMessageBox.Text = "We got it! Target has been destroyed!";
            //Clears target and set back to zero.
            xWrite.ClearTarget(0);
                      
        }
        


        
    }
}
